<template>
  <div style="padding: 6px;">
    <div class="clearfix">
      <el-button style="float: right" type="primary" size="small" icon="el-icon-plus" @click="addStudent">添加</el-button>
    </div>
    <el-table :data="stuObj.list" border style="width: 100%">
      <el-table-column label="姓名" prop="name"></el-table-column>
      <el-table-column label="年龄" prop="age"></el-table-column>
      <el-table-column label="生日">
        <template v-slot="scope">
          <!--<span>{{scope.$index}}</span>-->
          <DateFormat :value="scope.row.birth" :format="'YYYY-MM-DD HH:mm'"></DateFormat>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button size="mini" type="primary" @click="editStudent(scope.row.id)">修改</el-button>
          <el-button size="mini" type="danger"  @click="deleteStudent(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="center" background layout="total, prev, pager, next"
                   @current-change="loadStudent" :total="stuObj.total"
                   :current-page.sync="sd.pageNum" :page-size="sd.pageSize">
    </el-pagination>

    <el-dialog :title="popupTitle" :visible.sync="entityDetailVisible" width="800px">
      <studentDetail v-if="entityDetailVisible" :visible.sync="entityDetailVisible" :entityId="popupEntityId"></studentDetail>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'
import studentDetail from '@/views/basedata/student/studentDetail'

export default {
  components: { studentDetail },
  data() {
    return {
      sd: {
        pageNum: 1,
        pageSize: 5
      },
      stuObj: {},

      entityDetailVisible : false,
      popupTitle : '',
      popupEntityId : 0,
    }
  },
  mounted() {
    this.loadStudent()
    this.$EventBus.$on("stuadd", () => {
      this.sd.pageNum = 1;
      this.loadStudent();
    });
    this.$EventBus.$on("stuedit", () => {
      this.loadStudent();
    });
  },
  beforeDestroy() {
    this.$EventBus.$off("stuadd")
    this.$EventBus.$off("stuedit")
  },
  methods: {
    loadStudent() {
      request({
        url: '/student/pagestudent',
        data: this.sd
      }).then(res => {
        const { data } = res
        this.stuObj = data
      })
    },
    addStudent(){
      this.popupEntityId = 0;
      this.popupTitle= "新增学生"
      this.entityDetailVisible= true
    },
    editStudent(id){
      this.popupEntityId= id;
      this.popupTitle= "修改学生"
      this.entityDetailVisible= true
    },
    deleteStudent(id){
      request({
        url: '/student/deletestudent',
        data: {id : id}
      }).then(res => {
        if(res.code === '200') {
          this.$message.success(res.msg)
          this.loadStudent();
        }
      })
    }

  }
}
</script>
